home *** CD-ROM | disk | FTP | other *** search
/ Plug-In Power Pack for Netscape Communicator / Plug-In Power Pack for Netscape Communicator.iso / plugins / dataviews / dvtools / examples / programs / popup.c < prev    next >
Encoding:
C/C++ Source or Header  |  1997-05-08  |  15.9 KB  |  453 lines

  1. #ifndef lint
  2. static char SccsId[]= "@(#)popup.c    V1.18    3/13/95";
  3. #endif
  4. /*
  5. |    file name - popup.c
  6. |===================================================================
  7. |
  8. |       Example program that shows one method of doing "pop-up" inputs.
  9. |
  10. |    This method uses two views created using DV-Draw.  One view
  11. |    was initially created with inputs and outputs together.
  12. |    "Save Sub View" was used to create separate input and
  13. |       output views.
  14. |
  15. |    The output view (editatts.v) contains named "poke" objects.
  16. |    When these objects ("LineMenu" & "ColorPalette") are selected
  17. |    the appropriate pop-up input is drawn.  The menu and palette
  18. |    control the line and color attributes of "rect.obj".
  19. |
  20. |    The input view (attsinputs.v) contains the menu and palette
  21. |    input object.  The application posts a ServiceResult function
  22. |    which changes rect.obj's attributes and erases the pop-up input.
  23. |    The input objects both have key bindings as follows...
  24. |
  25. |        DONE_KEYS < LEFT_MOUSE, s>
  26. |        CANCEL_KEYS  < MIDDLE_MOUSE, RIGHT_MOUSE, c>
  27. |
  28. |    Selecting the RIGHT_MOUSE button outside of a pop-up menu
  29. |    causes both input objects to be erased.
  30. |
  31. |    Typing a <q|Q> will exit the program.
  32. |
  33. |===================================================================
  34. */
  35. #include <windows.h>
  36.  
  37.  
  38. /*
  39.  *  DV-Tools header files
  40.  */
  41. #include "std.h"                /* <stdio.h> etc., scalar & macro definitions */
  42. #include "dvstd.h"              /* public types & constants */
  43. #include "dvtools.h"            /* constants used by T routines */
  44. #include "dvGR.h"               /* constants used by window mgt & GR routines */
  45. #include "VOstd.h"              /* constants used by VO & VOob routines */
  46. #include "Tfundecl.h"           /* T routines (screens, drawports & views) */
  47. #include "VOfundecl.h"          /* VO routines (objects) */
  48. #include "VUerfundecl.h"        /* VUer routines (event handling routines) */
  49.  
  50. /* Constants */
  51. #define  DVPATH                (char *)NULL
  52. #define  DISPFORMS_STB         (char *)NULL
  53. #define  DVDEVICE              (char *)NULL
  54. #define  DVCOLORTABLE          (char *)NULL
  55. #define  VIEW_NAME             "editatts.v"
  56. #define  SCREEN_VIEWPORT       (RECTANGLE *)NULL
  57. #define  CLIENT_LINE_MENU      (OBJECT)1
  58. #define  CLIENT_COLOR_PALETTE  (OBJECT)2
  59.  
  60. /* Buffer addresses of data source variables used by popups. */
  61. float *LineData;
  62. float *ColorData;
  63.  
  64. /* information passed to the service routine HandleInput() */
  65. typedef struct
  66. {
  67.   DRAWPORT drawport;       /* how & where to display picture, picture frame */
  68.   OBJECT rect_obj;         /* rectangle object to affect attributes */
  69.   OBJECT input[2];         /* popup input objects */
  70. } INFO;
  71.  
  72. /* Functions defined in popup.c */
  73. int HandleInput V_P_((OBJECT client, EVENT_REQUEST er, int label,
  74.               OBJECT loc, ADDRESS info));  /* service result routine */
  75.  
  76. /*
  77.  *   MAIN PROGRAM
  78.  */
  79. int APIENTRY WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
  80.                      LPSTR lpCmdLine,  int nCmdShow  )
  81. {
  82.   /*
  83.    *  program arguments
  84.    *    argv[1] - display device (default is DVDEVICE)
  85.    */
  86.  
  87.   /* Define & initialize device name and view filename */
  88.   char *device_name = DVDEVICE; /* default device name */
  89.   char *view_name = VIEW_NAME;  /* default view name */
  90.  
  91.   /* Define display variables */
  92.   OBJECT screen;                /* display device, the window */
  93.   VIEW   view;                  /* picture representation of the view file */
  94.  
  95.   /* Control loop variables */
  96.   OBJECT location;              /* the event representation */
  97.  
  98.   /* Input object related variables */
  99.   VIEW     input_view;          /* picture representation of popups */
  100.   OBJECT   input_drawing;       /* graphical representation of popups */
  101.   ADDRESS *vdplist;             /* variable descriptor list for menu */
  102.   int      numvars;             /* number of vdps in list */
  103.   DSVAR    dsvar;               /* data source variable bound to vdp */
  104.   INFO     info;                /* struct passed to service result routine */
  105.   char     *popup_name;         /* name of popup input object */
  106.  
  107.   /* Other variables */
  108.   OBJECT   main_drawing;        /* graphical representation of main view */
  109.   int      Quit = NO;           /* flag to exit the program */
  110.  
  111.   /* Whole world rectangle, (-16384, -16384) to (16383, 16383)*/
  112.   LOCAL RECTANGLE whole_world = { XMIN, YMIN, XMAX, YMAX };
  113.   int argc;
  114.   char **argv;
  115.  
  116.  
  117.   make_argv(&argc,&argv,GetCommandLine());
  118.  
  119.   /*-----------------
  120.    *   Initialization
  121.    *
  122.    *   TInit:    perform the initialization of DV-Tools
  123.    *             TInit reads your configuration file and any
  124.    *             environment variables or logical names set.
  125.    */
  126.   TInit (DVPATH, DISPFORMS_STB);
  127.  
  128.   /*
  129.    *   TscOpenSet:  opens a device as a screen object using
  130.    *                specified attributes
  131.    */
  132.   if (argc > 1)
  133.     device_name = argv[1];
  134.   screen = TscOpenSet (device_name, DVCOLORTABLE,
  135.                        V_WINDOW_NAME, "popup",
  136.                        V_X_EXPOSURE_BLOCK, YES,
  137.                        V_ACTIVE_CURSOR, V_END_OF_LIST);
  138.   if (!screen)
  139.     {
  140.       printf ("Must specify device on command line or");
  141.       printf (" in DataViews configuration file.\n");
  142.       S_EXIT (EXIT_ERR);
  143.     }
  144.  
  145.   /*
  146.    *   VOscWinEventMask:  sets the screen's window event mask
  147.    */
  148.   VOscWinEventMask ((ULONG) V_KEYPRESS | V_BUTTONPRESS | V_EXPOSE | V_RESIZE,
  149.             (ULONG) 0);
  150.  
  151.   /*
  152.    *   TviLoad:   Load a view in from a file,
  153.    *   TdpCreateStretch: Create a drawport with stretched coordinates
  154.    *              The drawport is attached to the screen object
  155.    *              specified while view specifies the view to be
  156.    *              displayed on the screen.
  157.    *              Objects of the view are stretched to make the
  158.    *              the portion of the view specified as whole_world
  159.    *              fit in the specified viewport, SCREEN_VIEWPORT.
  160.    */
  161.   view = TviLoad (view_name);
  162.   if (!view)
  163.     {
  164.       printf ("Could not load view from file ");
  165.       printf ("%s.\n", view_name);
  166.       S_EXIT (EXIT_ERR);
  167.     }
  168.   info.drawport = TdpCreateStretch (screen, view,
  169.                                     SCREEN_VIEWPORT, &whole_world);
  170.  
  171.   /*
  172.    *   TviGetDrawing:     Gets a view's drawing object
  173.    *   TdrGetNamedObject: Gets the name of an object from
  174.    *                      a drawing.
  175.    *
  176.    *   Get the object named "rect.obj" from the view's drawing.
  177.    */
  178.   main_drawing = TviGetDrawing (view);
  179.   info.rect_obj = TdrGetNamedObject (main_drawing, "rect.obj");
  180.  
  181.   /*
  182.    *   TscErase: Erase the entire screen in the default
  183.    *             background color
  184.    *   TdpDraw:  Draw the contents of the drawport
  185.    */
  186.   TscErase (screen);
  187.   TdpDraw (info.drawport);
  188.  
  189.   /*
  190.    *   TviLoad:         Load a view in from a file,
  191.    *   TviGetDrawing:     Gets a view's drawing object
  192.    *   TdrGetNamedObject: Gets the name of an object from
  193.    *                      a drawing.
  194.    *
  195.    *   Get the popup input objects from the view's drawing.
  196.    *   One input object is a menu of line types and the other
  197.    *   is a color palette.
  198.    */
  199.   input_view = TviLoad ("attsinputs.v");
  200.   input_drawing = TviGetDrawing (input_view);
  201.   info.input[0] = TdrGetNamedObject (input_drawing, "LineMenu.input");
  202.   info.input[1] = TdrGetNamedObject (input_drawing, "ColorPalette.input");
  203.  
  204.   /*
  205.    *   VOinGetVarList:  Gets a variable descriptor list of
  206.    *                    the input object.
  207.    *   TvdGetDataSourceVariable:  Gets the data source variable
  208.    *                              which the variable descriptor
  209.    *                              (vdp) is linked to.
  210.    *   TdsvGetBuffer:   Gets data source variable (dsv)
  211.    *                    buffer address.
  212.    *
  213.    *   Obtain the vdp list for the each input object.
  214.    *   Find the data source variable which the vdp is linked
  215.    *   to. Then obtain the buffer address which the data
  216.    *   source variable is pointing to.
  217.    */
  218.   VOinGetVarList (info.input[0], &vdplist, &numvars);
  219.   dsvar = TvdGetDataSourceVariable (vdplist[0]);
  220.   LineData = (float *) TdsvGetBuffer (dsvar);
  221.  
  222.   VOinGetVarList (info.input[1], &vdplist, &numvars);
  223.   dsvar = TvdGetDataSourceVariable (vdplist[0]);
  224.   ColorData = (float *) TdsvGetBuffer (dsvar);
  225.  
  226.   /*
  227.    *   VUerServiceResultPost:  Post a service result request
  228.    *                           with the event handler.
  229.    *
  230.    *   Post a service result request which monitors the
  231.    *   line menu and color palette input objects. Two
  232.    *   requests are posted for each input object. One specifies
  233.    *   the type of the service result flag to be generated as
  234.    *   an INPUT_DONE.  INPUT_DONE indicates an input sequence
  235.    *   sequence has been completed (a menu selection was made).
  236.    *   The other request specifies INPUT_CANCEL as the service
  237.    *   result flag to be serviced. INPUT_CANCEL indicates the
  238.    *   the user cancel the input.
  239.    */
  240.   VUerServiceResultPost ((OBJECT) CLIENT_LINE_MENU,
  241.                    (VUERFCNFUNPTR)HandleInput, (ADDRESS) & info, (int)sizeof (info),
  242.                    info.input[0], (int) INPUT_DONE, (int) INPUT_DONE);
  243.  
  244.   VUerServiceResultPost ((OBJECT) CLIENT_LINE_MENU,
  245.                    (VUERFCNFUNPTR)HandleInput, (ADDRESS) & info, (int)sizeof (info),
  246.                    info.input[0], (int) INPUT_CANCEL, (int) INPUT_CANCEL);
  247.  
  248.   VUerServiceResultPost ((OBJECT) CLIENT_COLOR_PALETTE,
  249.                    (VUERFCNFUNPTR)HandleInput, (ADDRESS) & info, (int)sizeof (info),
  250.                    info.input[1], (int) INPUT_DONE, (int) INPUT_DONE);
  251.  
  252.   VUerServiceResultPost ((OBJECT) CLIENT_COLOR_PALETTE,
  253.                    (VUERFCNFUNPTR)HandleInput, (ADDRESS) & info, (int)sizeof (info),
  254.                    info.input[1], (int) INPUT_CANCEL, (int) INPUT_CANCEL);
  255.  
  256.   /*--------------------
  257.    *   Control loop
  258.    *
  259.    *   Poll the event queue for locator events. If the key
  260.    *   represents the character 'q' or 'Q' then quit the program.
  261.    *   Check for a selection of a popup menu.
  262.    *   Events occurring within input objects will be handled
  263.    *   through the event request handler VUerHandleLocEvent.
  264.    */
  265.   FOREVER
  266.   {
  267.     /*
  268.      *  VOloWinEventPoll:  Poll for the next window event.
  269.      *                     The polling mode used is V_WAIT.
  270.      *                     Therefore, VOloWinEventPoll does not
  271.      *                     return until a masked event is
  272.      *                     generated.  V_WAIT always produces
  273.      *                     a valid location object.
  274.      *  VUerHandleLocEvent: Service the event.
  275.      *                      This routine will check if the
  276.      *                      event is used by any input objects
  277.      *                      that are in the view.
  278.      */
  279.     location = VOloWinEventPoll (V_WAIT);
  280.     VUerHandleLocEvent (location);
  281.  
  282.     /*
  283.      *  VOloType:  returns the type of event.  These types
  284.      *             match event types specified in VOscWinEventMask.
  285.      */
  286.     switch (VOloType (location))
  287.       {
  288.  
  289.       case V_RESIZE:
  290.         /*
  291.          *  The window size has been changed.
  292.          *  TscReset:  Resets all screen drawports after
  293.          *             window resizing
  294.          */
  295.         TscReset (screen);
  296.         break;
  297.  
  298.       case V_EXPOSE:
  299.         /*
  300.          *  VOloRegion:  Returns a rectangle representing the
  301.          *               exposed region on the screen.
  302.          *  TscRedraw:   After erasing, redraws all the drawports
  303.          *               in the screen.
  304.          *  A portion of the window has been exposed and needs
  305.          *  to be redrawn.
  306.          */
  307.         TscRedraw (screen, VOloRegion (location));
  308.         break;
  309.  
  310.       case V_KEYPRESS:
  311.         /*
  312.          *  Check key selected.
  313.          *  VOloKeySym:  Returns the key symbol value of the
  314.          *               location object
  315.          *
  316.          *  If the key symbol represents the characters 'q'
  317.          *  or 'Q' then quit the program.
  318.          */
  319.         switch (VOloKeySym (location))
  320.           {
  321.           case 'q':
  322.           case 'Q':
  323.             Quit = YES;
  324.             break;
  325.  
  326.           default:
  327.             break;
  328.           }
  329.  
  330.       case V_BUTTONPRESS:
  331.         /*
  332.          *  VOloButton:  Returns the button that was pressed
  333.          */
  334.         if (VOloButton (location) == 3)
  335.           {
  336.             TdpEraseObject (info.drawport, info.input[0]);
  337.             TdpEraseObject (info.drawport, info.input[1]);
  338.           }
  339.         else
  340.           /*
  341.            *  TloGetSelectedObjectName:  Get the name of the
  342.            *                  selected object.
  343.            *  TdpDrawObject:  Draw a specific object within a drawport.
  344.            *
  345.            *  Draw the popup input object when selected.  The object
  346.            *  name is used to identify the object.
  347.            */
  348.         popup_name = TloGetSelectedObjectName (location);
  349.         if (popup_name)
  350.           {
  351.             if (strcmp ("LineMenu", popup_name) == 0)
  352.               TdpDrawObject (info.drawport, info.input[0]);
  353.             else if (strcmp ("ColorPalette", popup_name) == 0)
  354.               TdpDrawObject (info.drawport, info.input[1]);
  355.           }
  356.         break;
  357.  
  358.       default:
  359.         break;
  360.       }
  361.  
  362.     /* exit the program */
  363.     if (Quit == YES)
  364.       break;
  365.   }
  366.  
  367.   /*--------------------
  368.    *   Termination
  369.    *
  370.    *   VUerClearAll: Clears a client's event requests
  371.    *   TdpDestroy:   Destroy the drawport,
  372.    *   TviDestroy:   Destroy the view, freeing the allocated memory
  373.    *   TscCloseCurrentScreen:  Close the current display screen
  374.    *   TTerminate:   Perform the clean-up for DV-Tools
  375.    */
  376.   VUerClearAll ((OBJECT) CLIENT_LINE_MENU);
  377.   VUerClearAll ((OBJECT) CLIENT_COLOR_PALETTE);
  378.   TdpDestroy (info.drawport);
  379.   TviDestroy (view);
  380.   TviDestroy (input_view);
  381.   TscCloseCurrentScreen ();
  382.   TTerminate ();
  383.   return EXIT_OK;
  384. }
  385.  
  386.  
  387. /*--------------
  388.  *   HandleInput -- Perform the appropriate action based
  389.  *      on the user's selection from the popup input object.
  390.  *      Change the attributes of the object based on the
  391.  *      selection from the popup.
  392.  */
  393. /*ARGSUSED*/
  394. int 
  395. HandleInput (client, er, label, loc, args)
  396.      OBJECT client;
  397.      EVENT_REQUEST er;
  398.      int label;
  399.      OBJECT loc;
  400.      ADDRESS args;
  401. {
  402.   ATTRIBUTES atts;              /* Attribute structure for objects */
  403.   INFO *info = (INFO *)args;
  404.  
  405.   /*
  406.    *   TdpEraseObject:  Erase an object within a drawport
  407.    *            erase popup input object which is
  408.    *            being serviced.
  409.    */
  410.   TdpEraseObject (info->drawport, info->input[client - 1]);
  411.  
  412.   /*
  413.    *   VOobAtGet:      Get the current attributes of an object
  414.    *   VOobAtSet:      Set new attributes in an object
  415.    *   TdpDrawObject:  Draw a specific object within a drawport
  416.    *
  417.    *   Erase the rectangle object from the main drawport.
  418.    *   Then obtain the current attributes of the rectangle
  419.    *   object.  If the user selected from the line menu
  420.    *   check the value of the data buffer. A value
  421.    *   greater than four will affect the line width used
  422.    *   whereas a value of 1 - 4 will modify the pattern
  423.    *   used for the line. If the user selected from the
  424.    *   color palette then the foreground color of the
  425.    *   rectangle object will be modified.  Set the
  426.    *   attributes and draw the object with the new
  427.    *   attributes.
  428.    */
  429.   if (label == INPUT_DONE)
  430.     {
  431.       TdpEraseObject (info->drawport, info->rect_obj);
  432.       VOobAtGet (info->rect_obj, &atts);
  433.       switch (client)
  434.         {
  435.         case CLIENT_LINE_MENU:
  436.           if (*LineData > 4)
  437.             atts.line_width = (char) (*LineData - 4);
  438.           else
  439.             atts.line_type = (char) (*LineData);
  440.           break;
  441.  
  442.         case CLIENT_COLOR_PALETTE:
  443.           atts.foreground_color = VOcoCreate (COLOR_INDEX,
  444.                               (LONG) * ColorData, (LONG) 0, (LONG) 0);
  445.           break;
  446.         }
  447.       VOobAtSet (info->rect_obj, &atts);
  448.       TdpDrawObject (info->drawport, info->rect_obj);
  449.     }
  450.  
  451.   return (int) INPUT_USED;
  452. }
  453.